#
# Kurella/Rapp: The role of issue salience and competitive advantages in spatial models of political competition
#
# Replication Material: Figure 6, Table 3
#
#
######################################

################################################################################
#
# Load required Packages 
#

library(dplyr)
library(haven)
library(stargazer)
library(ggpubr)
library(ggplot2)
library(ggeffects)

################################################################################
#
# Read data
#

ndata_long <- read_dta("ReplicationData_MacroAnalysis.dta")


################################################################################
#
# Estimation Model 1 - Model 5
#



# Model 1: Base
m_1 <- lm(right_party_vote ~    sal_immi_vote +
              cntry_pty ,
             data=ndata_long)
          
summary(m_1)




# Model 2: Extremeness 
m_2 <- lm(right_party_vote ~    sal_immi_vote +
               sal_immi_vote*right_dist_immi + 
             cntry_pty ,
           data=ndata_long)

summary(m_2)


# Model 3: Moderateness
m_3 <- lm(right_party_vote ~    sal_immi_vote +
             sal_immi_vote*right_dist_econ +
             cntry_pty ,
           data=ndata_long)

summary(m_3)


# Model 4: Uniqueness
m_4 <- lm(right_party_vote ~    sal_immi_vote +
              sal_immi_vote*right_dist_compet +
             cntry_pty ,
           data=ndata_long)

summary(m_4)



# Model 5: Full model
m_5<- lm(right_party_vote ~    sal_immi_vote +
             sal_immi_vote*right_dist_compet +
             sal_immi_vote*right_dist_immi + 
             sal_immi_vote*right_dist_econ +
             cntry_pty ,
         data=ndata_long)

summary(m_5)


################################################################################
# Figure 6

# a) Extremeness
p1 <- ggpredict(m_5,
                terms = c("sal_immi_vote","right_dist_immi[0.2,1]"),
                ci.lvl = 0.95)

p11 <- as.data.frame(p1)
p11$group <- as.factor(p11$group)

plot_extreme  <- ggplot(data=p11, aes(x=x, y=predicted, color=group))+ 
  geom_line(aes(color=group), lwd=1.2)+ 
  
  geom_ribbon(aes(ymin=p1$conf.low, ymax=p1$conf.high,color=group), lty=2, alpha=0.01)+ #CIs
  scale_colour_manual(values=c("darkblue", "lightblue" )) + 

  labs(
    title="",
    x= "Salience", 
    y= "Core issue party vote share", 
    color= "Extremeness on core issue", 
    fill="Extremeness on core issue", 
    linetype="Extremeness on core issue")+ 
  theme_classic()+ 
  theme(
    element_line(colour="black"),
    text=element_text(size=17), legend.position="top") 



# b) Economic Moderation
p2 <- ggpredict(m_5,
                terms = c("sal_immi_vote","right_dist_econ[0,0.9]"),
                ci.lvl = 0.95)

p12 <- as.data.frame(p2) 
p12$group <- as.factor(p12$group)

plot_econ  <- ggplot(data=p12, aes(x=x, y=predicted, color=group))+ 
  geom_line(aes(color=group), lwd=1.2)+ #adding lines
  
  geom_ribbon(aes(ymin=p2$conf.low, ymax=p2$conf.high,color=group), lty=2, alpha=0.01)+ #CIs
  scale_colour_manual(values=c("darkblue", "lightblue" )) + 
  labs( 
    title="", 
    x= "Salience", 
    y= "Core issue party vote share", 
    color= "Extremeness on econ issue", 
    fill="Extremeness on econ issue", 
    linetype="Extremeness on econ issue")+ 
  theme_classic()+ 
  theme(
    element_line(colour="black"),
    text=element_text(size=17), legend.position="top") 


# c) Uniqueness
p3 <- ggpredict(m_5,
                terms = c("sal_immi_vote", "right_dist_compet[0,1]"),
                ci.lvl = 0.95)

p13 <- as.data.frame(p3) 
p13$group <- as.factor(p13$group) 

plot_dist  <- ggplot(data=p13, aes(x=x, y=predicted, color=group))+ 
  geom_line(aes(color=group), lwd=1.2)+ 
  
  geom_ribbon(aes(ymin=p3$conf.low, ymax=p3$conf.high,color=group), lty=2, alpha=0.01)+ #CIs
  scale_colour_manual(values=c("lightblue", "darkblue")) + 
  labs( 
    title="", 
    x= "Salience", 
    y= "Core issue party vote share", 
    color= "Distance to competitor", 
    fill="Distance to competitor", 
    linetype="Distance to competitor")+ 
  theme_classic()+ 
  theme(
    element_line(colour="black"),
    text=element_text(size=17), legend.position="top") 


# arrange plots
ggarrange(plot_extreme, plot_econ, plot_dist ,  
          font.label = list(size = 20, face = "bold"),
          labels = c("a) Extremeness", "b) Economic Moderation", "c) Uniqueness"),
          ncol = 3, nrow = 1)


################################################################################
# Table 3

stargazer(m_1,m_2,m_3, m_4, m_5,
          header=F, 
          type= "latex", dep.var.labels = "Core issue party vote share",
          title= "Core issue party vote", notes = "standard errors in parentheses")





################################################################################
# Table 1 Appendix

table(ndata_long$cntry_wave, ndata_long$party)




